package com.lhz.collector.service;

import com.lhz.collector.entity.Collector;
import com.lhz.collector.entity.CollectorRuleRef;
import com.lhz.collector.entity.ParseRule;
import com.lhz.collector.mapper.CollectorMapper;
import com.lhz.collector.mapper.CollectorRuleRefMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by LHZ on 2017/4/1.
 */
@Service
public class CollectorServiceImpl implements CollectorService{
    private static final Logger logger= LoggerFactory.getLogger(CollectorServiceImpl.class);
    @Autowired
    private CollectorMapper collectorMapper;
    @Autowired
    private CollectorRuleRefMapper collectorRuleRefMapper;

    /**
     * 添加采集器，先添加引用的解析规则
     * @param collector
     * @return
     */
    @Transactional
    @Override
    public boolean add(Collector collector) {
        try {
            collectorMapper.insert(collector);
            long collectorTd=collector.getId();
            List<ParseRule> parseRules=collector.getParseRules();
            if(parseRules!=null&&parseRules.size()>0){
                for(ParseRule parseRule:parseRules){
                    long parseRuleId=parseRule.getId();
                    if(parseRuleId!=0){
                        CollectorRuleRef collectorRuleRef=new CollectorRuleRef(collectorTd,parseRuleId);
                        collectorRuleRefMapper.insert(collectorRuleRef);
                    }
                }
            }
            return true;
        }catch (Exception e){
            logger.error("采集器添加失败",e);
            throw new RuntimeException(e);
        }
    }
    @Transactional
    @Override
    public boolean delete(long id) {
        try{
            collectorRuleRefMapper.deleteByCollectorId(id);
            collectorMapper.deleteById(id);
            return true;
        }catch (Exception e){
            logger.error("采集器：{}删除失败",id,e);
            throw new RuntimeException(e);
        }
    }
    @Transactional
    @Override

    public boolean update(Collector collector) {
        long collectorTd=collector.getId();
        try{
            collectorRuleRefMapper.deleteByCollectorId(collectorTd);
            List<ParseRule> parseRules=collector.getParseRules();
            collectorMapper.update(collector);
            if(parseRules!=null&&parseRules.size()>0){
                for(ParseRule parseRule:parseRules){
                    long parseRuleId=parseRule.getId();
                    if(parseRuleId!=0){
                        CollectorRuleRef collectorRuleRef=new CollectorRuleRef(collectorTd,parseRuleId);
                        collectorRuleRefMapper.insert(collectorRuleRef);
                    }
                }
            }
            return true;
        }catch (Exception e){
            logger.error("采集器：{}修改失败",collectorTd,e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Collector findById(long id) {
        return collectorMapper.findById(id);
    }

    @Override
    public List<Collector> findAll() {
        List<Collector> list=collectorMapper.findAllList(new Collector());
        return list;
    }
}
